var chat_Team1=null;
var chat_Team2=null;

function chatOpen(team1,team2){
	chat_Team1=team1;
	chat_Team2=team2;
	localStorage.setItem('chat_team1',chat_Team1);
	localStorage.setItem('chat_team2',chat_Team2);
	displayTeam();
}
function chatOpenTeam(data){
	var chat_teams=data.split( ' vs ');
	chat_Team1= chat_teams[0];
	chat_Team2= chat_teams[1];
	localStorage.setItem('chat_team1',chat_Team1);
	localStorage.setItem('chat_team2',chat_Team2);
	displayTeam();
}
function chatNo(){
	console.log('no_chat');
	localStorage.setItem('chat_team1','null');
	localStorage.setItem('chat_team2','null');
	displayTeam();
}
function displayTeam(){
	if((localStorage.getItem('chat_team1')=='null')){
		console.log('display'+chat_Team1);
		$(".chatteams").empty();
		$('<span>오늘은 경기가 없습니다.</span>').appendTo($('.chatteams'));
	}else{
		$(".chatteams").empty();
		$('<span>'+chat_Team1+'vs'+chat_Team2+'</span>').appendTo($('.chatteams'));
	}
}

$(function(){
	alert('chat.js ready');
	var chatName=null;
	var socket=io.connect('http://70.12.110.71:7070');
	
	$('#btn_chat').on('click',function() {
		console.log('chat_Team1'+chat_Team1);
		console.log('chat_Team2'+chat_Team2);	
			
			
			$('#result_chat').empty();
			var chat_data = chat_Team1 +" vs "+chat_Team2;
			$( "<h1>"+chat_data+"</h1>").appendTo($('#result_chat'));
			
			
			var team1=localStorage.getItem('chat_team1').substring(0,2);
			var team2=localStorage.getItem('chat_team2').substring(0,2);
			var chatMyTeam=localStorage.getItem('myTeam').substring(0,2);
			
			if(localStorage.getItem("teamId")=="258"){
				chatMyTeam="맨시";
			}else if(localStorage.getItem("teamId")=="246"){
				chatMyTeam="맨유";
			}
			
			console.log('chatMyTEam'+chatMyTeam);
			console.log('chat1'+team1);
			console.log('chat2'+team2);
			
			if(chatMyTeam==team1){
				chatName='A';
			}else if(chatMyTeam==team2){
				chatName='B';
			}
			
			var roomName=team1+team2;
			console.log("roomName : "+roomName);
			var data={chatName : chatName, roomName : roomName};
			
			if(!(roomName=='nunu')){
				
				socket.emit('entry',JSON.stringify(data));			
			}else{
				alert('오늘은 경기가 없습니다.');
			}
	});
 
   	 /* $('#chatNameButton').on('click',function(){
   		 var chatName=$('#chatNameText').val().trim();
   		 if(chatName.length<=0){
   		 chatName="unknown";
   		 }
   		 
   	  });*/
   	  
		socket.on('entry',function(chatName){
		// $.mobile.changePage( "#chatPage", { transition: "slideup", changeHash: false });    		 
			$(location).attr('href',"#chatPage");
		});
   	  
		$("#chatSendButton").on('click',function(){
   		  	 //now.distributeMessage($("#messageText").val());
            socket.emit('sendmsg',$("#messageText").val());
             $("#messageText").val("");
             $("#messageText").focus();
           
      });
   	  $("#messageText").on('keypress',function(evt){
   		if(evt.keyCode == 13)	{
   			//now.distributeMessage($("#messageText").val());
   			socket.emit('sendmsg',$("#messageText").val());
   			$("#messageText").val("");
   			$("#messageText").focus();
   			
   		}
   	  });
   	  
   	  
   	  socket.on('message',function(data){
   		 var msg = JSON.parse(data);
   		var time = new Date();
		
   		var minute = time.getMinutes();
		
		if(minute<="9"){
			var chat_time = time.getHours() + ":" + "0"+minute;
		}else{
			var chat_time = time.getHours() + ":" +minute;
		}
		
		if(msg.chatName=='A'){
			$( "<span class='messageA'><span class='chat_nameA'>"+chat_Team1+"</span>"+msg.text+"</span><span class='messageA_sub'>"+chat_time+"</span><br><br>").appendTo($('#incomingMessages'));
			$("#incomingMessages").scrollTop($("#incomingMessages")[0].scrollHeight);
			console.log($("#incomingMessages")[0].scrollHeight);
		

		}else if(msg.chatName=="B"){
			$("<span class='messageB'>"+msg.text+"<span class='chat_nameB'>"+chat_Team2+"</span></span><span class='messageB_sub'>"+chat_time+"</span><br><br>").appendTo($('#incomingMessages'));
			// $("#incomingMessages").append("<div class='message'><span class='username'>" + msg.chatName + ":</span> " + msg.text + "</div>");
			$("#incomingMessages").scrollTop($("#incomingMessages")[0].scrollHeight);
			//console.log($("#incomingMessages")[0].scrollHeight);
			//$.mobile.silentScroll("#incomingMessages")[0].scrollHeight), 100);
		}
	});                            
});